<?php

declare(strict_types=1);

namespace App\Http\Controllers\Manage;

use App\Repositories\Manage\MenuRepository;
use App\Repositories\Manage\UserRepository;

class MenusController extends ManageBaseController
{

    private $menuRepo = NULL,
        $userRepo = NULL;

    public function __construct(
        MenuRepository $menuRepo,
        UserRepository $userRepo,
    )
    {
        parent::__construct();
        $this->menuRepo = $menuRepo;
        $this->userRepo = $userRepo;
    }

    public function index()
    {
        $filter = request()->all();
        $menus = $this->menuRepo->getMenus($filter);
        return retSuc($menus);
    }

    public function routes()
    {
        if (request()->get("flag")) {
            $roleIds = $this->userRepo->getRolesById(UID);
            $res = $this->menuRepo->getMenuByRoleId($roleIds, ["MENU", "CATALOG"], UID);
            return retSuc($res);
        }

        return retSuc([
            [
                "name" => "/system",
                "path" => "/system",
                "redirect" => "/system/user",
                "component" => 'Layout',
                "meta" => [
                    'hidden' => false,
                    'icon' => "system",
                    'title' => "系统管理",
                    'roles' => ['ADMIN6', 'GUEST', 'ADMIN']
                ],
                'children' => [
                    [
                        "path" => "user",
                        "name" => "User",
                        "component" => 'system/user/index',
                        "meta" => [
                            'hidden' => false,
                            'icon' => "user",
                            "keepAlive" => true,
                            'title' => "用户管理",
                            'roles' => ['GUEST', 'ADMIN']
                        ],
                    ],
                    [
                        "path" => "role",
                        "name" => "Role",
                        "component" => 'system/role/index',
                        "meta" => [
                            'hidden' => false,
                            'icon' => "role",
                            "keepAlive" => true,
                            'title' => "角色管理",
                            'roles' => ['GUEST', 'ADMIN6', 'ADMIN']
                        ],
                    ],
                    [
                        "path" => "menu",
                        "name" => "Menu",
                        "component" => 'system/menu/index',
                        "meta" => [
                            'hidden' => false,
                            'icon' => "menu",
                            "keepAlive" => true,
                            'title' => "菜单管理",
                            'roles' => ['ADMIN']
                        ],
                    ]
                ]
            ],
        ]);
    }

    public function options()
    {
        $menus = $this->menuRepo->getOptions();

        return retSuc($menus);
    }

    public function detail($id = 0)
    {
        $id = intval($id);
        if (!$id)
            return retFail("参数错误");

        $res = $this->menuRepo->getOne($id);

        return retSuc($res);
    }

    public function update($id = null)
    {
        $data = request()->all();
        $last_id = $this->menuRepo->saveOrUpdate($data, $id ?? 0);
        if (!$last_id) {
            return retFail("更新失败");
        }

        $menu = $this->menuRepo->getOne($last_id);
        return retSuc($menu);
    }

    public function delete($id)
    {
        $ids = array_filter(array_unique(explode(",", $id)));

        if (empty($ids)) {
            return retFail("参数错误");
        }

        return $this->menuRepo->batchDelete($ids);
    }
}
